package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.TeacherRatingLimitJdbc;
import com.wuseguang.springcloud.dao.repository.SchoolRatingLimitsRrpository;
import com.wuseguang.springcloud.dao.repository.SchoolRatingLimitsSpecialRepository;
import com.wuseguang.springcloud.dao.repository.TeacherRatingLimitRepository;
import com.wuseguang.springcloud.entity.SchoolRatingLimits;
import com.wuseguang.springcloud.entity.SchoolRatingLimitsSpecial;
import com.wuseguang.springcloud.entity.TeacherRatingLimit;
import com.wuseguang.springcloud.service.TeacherRatingLimitService;
import com.wuseguang.springcloud.util.Result;
import com.wuseguang.springcloud.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * <p>
 * 教师评分限制表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-06-24
 */
@Service
public class TeacherRatingLimitServiceImpl  implements TeacherRatingLimitService {


    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private TeacherRatingLimitRepository teacherRatingLimitRepository;
    @Autowired
    private TeacherRatingLimitJdbc teacherRatingLimitJdbc;
    @Autowired
    private SchoolRatingLimitsRrpository schoolRatingLimitsRrpository;
    @Autowired
    private SchoolRatingLimitsSpecialRepository schoolRatingLimitsSpecialRepository;


    @Override
    public Result addTeacherRatingLimit(String schoolId, String teacherScore, Integer classTeacherScore) {

        //删除学校的评分限制
        teacherRatingLimitRepository.deleteBySchoolId(Integer.valueOf(schoolId));
        schoolRatingLimitsRrpository.deleteBySchoolId(Integer.valueOf(schoolId));
        //如果两个为-1就不添加限制
        if("0".equals(teacherScore) &&classTeacherScore<0){
            return new Result();
        }
        //特殊分数
        List<SchoolRatingLimitsSpecial> specials = new ArrayList<>();

        //特殊角色分数
        Map<String,Object> roleMap = new HashMap<>();
        //拆分分数
        String [] score= teacherScore.split(",");
        //循环分数
        for(int i = 1;i<score.length;i++){
            logger.info("添加特殊分数集合");
            String [] roleIdAndScore = score[i].split("-");
            roleMap.put(roleIdAndScore[0],roleIdAndScore[1]);
            //创建特殊分数
            SchoolRatingLimitsSpecial special = new SchoolRatingLimitsSpecial();
            special.setRoleId(Integer.valueOf(roleIdAndScore[0]));
            special.setScore(Integer.valueOf(roleIdAndScore[1]));
            specials.add(special);
        }


        //查询教师的添加评分限制信息 grouping_id=4/任课老师，grouping_id=3/班主任
        String sql = "select tr.pid classId,teacher_id teacherId,r.grouping_id groupingId ,r.id roleId from teacher_role tr " +
                "left join roles r on(tr.role_id=r.id) where  r.type=1 " +
                "and (r.grouping_id = 3 or r.grouping_id=4) and  tr.school_id= ? and tr.type = 3 ";
        //教师以及班级信息跟觉得信息
        List<Map<String,Object>> list =  teacherRatingLimitJdbc.queryListMap(sql,schoolId);
        //循环教师信息
        for (Map<String,Object> map : list) {
            //班级id
            String classId = map.get("classId").toString();
            //教师id
            String teacherId = map.get("teacherId").toString();
            //4=任课老师，3=班主任
            String groupingId = map.get("groupingId").toString();

            //教师评分限制
            TeacherRatingLimit trl =teacherRatingLimitRepository.findByClassIdAndTeacherId(Integer.valueOf(classId),Integer.valueOf(teacherId));
            //如果评分限制为空
            if(trl==null){
                logger.info("评分限制为空");
                trl = new TeacherRatingLimit();
                trl.setClassId(Integer.valueOf(classId));
                trl.setTeacherId(Integer.valueOf(teacherId));
            }

            trl.setSchoolId(Integer.valueOf(schoolId));
            //任课老师
            if("4".equals(groupingId)){
                logger.info("任课老师");
                //判断当前老师的所在班级的角色为特殊角色
                if(roleMap.containsKey(map.get("roleId"))){
                    //特殊分数
                    Integer tesu =Integer.valueOf(roleMap.get(map.get("roleId").toString()).toString());
                    logger.info("特殊角色");
                    //剩余分数添加
                    trl.setRemainingScore(trl.getRemainingScore()+tesu);
                    //总分添加
                    trl.setTotalScore(trl.getTotalScore()+tesu);
                    //任课教师分数
                    trl.setTeacherScore(trl.getTeacherScore()+tesu);
                }else{
                    //剩余分数添加
                    trl.setRemainingScore(trl.getRemainingScore()+Integer.valueOf(score[0]));
                    //总分添加
                    trl.setTotalScore(trl.getTotalScore()+Integer.valueOf(score[0]));
                    //任课教师分数
                    trl.setTeacherScore(trl.getTeacherScore()+Integer.valueOf(score[0]));

                }

            }else{
                logger.info("班主任");
                //剩余分数添加
                trl.setRemainingScore(trl.getRemainingScore()+classTeacherScore);
                //总分添加
                trl.setTotalScore(trl.getTotalScore()+classTeacherScore);
                //班主任分数
                trl.setClassTeacherScore(trl.getClassTeacherScore()+classTeacherScore);
            }

            teacherRatingLimitRepository.save(trl);
        }
        //添加学校评分限制，用于回显
        SchoolRatingLimits srl = new SchoolRatingLimits();
        srl.setAddTime(new Date());
        srl.setClassTeacherScore(classTeacherScore);
        srl.setTeacherScore(Integer.valueOf(score[0]));
        srl.setSchoolId(Integer.valueOf(schoolId));
        Integer id =schoolRatingLimitsRrpository.save(srl).getId();
        //添加
        specials.forEach(x -> x.setSchoolRatingLimitsId(id));
        schoolRatingLimitsSpecialRepository.saveAll(specials);

        return new Result();
    }
}
